// 书签栏中的js代码 src='http://127.0.0.1:7001/public/javascripts/cloud.js' 要考虑如何配置 目前直接在layout中写死的
// javascript:(function()%7Bvar t=window.location.href.split(':');if('http'==t%5B0%5D%7C%7C'https'==t%5B0%5D)%7Bdocument.getElementById('taosha_script')&&clearElement();var e=document.createElement('script');e.id='taosha_script',e.setAttribute('charset','utf-8'),e.src='http://127.0.0.1:7001/public/javascripts/cloud.js',document.body.insertBefore(e,document.body.childNodes%5B0%5D)%7D%7D)();


var url = encodeURIComponent(window.location.href);
var title = encodeURIComponent(document.title);

var host = 'https://taosha.club'; // 考虑如何配置

var TAOSHA_SCRIPT = 'taosha_script'; // 此脚本名称
var TAOSHA_BOX = 'taosha_box'; // 注入盒子名称
var TAOSHA_LOGIN_IFRAME = 'taosha_login'; // 登录iframe
var TAOSHA_COLLECT_IFRAME = 'taosha_collect'; // 收藏iframe
var TAOSHA_TOAST = 'taosha_toast'; // 淘沙提示框

// 创建最外层div
(function () {
  var box = document.createElement('div');
  box.id = TAOSHA_BOX;
  box.innerText = '加载中...';
  document.body.insertBefore(box, document.body.childNodes[0]);
  document.getElementById(TAOSHA_BOX).style.cssText = "display:block;position:fixed;top:20px;right:10px;z-index:2147483647;border:none;background-color:#f6f6f8;min-width:120px;min-height:50px;text-align:center;line-height: 50px";
  document.body.appendChild(box);
})();

// 创建登录页面iframe
(function () {
  var login = document.createElement('iframe');
  login.id = TAOSHA_LOGIN_IFRAME;
  login.width = '300px';
  login.height = '375px';
  login.src = host + "/cloud/login?url=" + url + "&title=" + title;
  document.getElementById(TAOSHA_BOX).appendChild(login);
  document.getElementById(TAOSHA_LOGIN_IFRAME).style.cssText = "display:none;position:fixed;top:20px;right:10px;z-index:2147483647;border:none;background-color:#424242";
})();

// 创建收藏页面iframe
(function () {
  var collect = document.createElement('iframe');
  collect.id = TAOSHA_COLLECT_IFRAME;
  collect.width = '520px';
  collect.height = '520px';
  collect.src = host + "/cloud/collect?url=" + url + "&title=" + title;
  document.getElementById(TAOSHA_BOX).appendChild(collect);
  document.getElementById(TAOSHA_COLLECT_IFRAME).style.cssText = "display:none;position:fixed;top:20px;right:10px;z-index:2147483647;border:none;background-color:#424242";
})();

// 成功提示框
(function () {
  var toast = document.createElement('div');
  toast.id = TAOSHA_TOAST;
  toast.innerHTML = '<div class="success-toast" style="display:flex;align-items:center;min-width:150px;height:50px;border-radius:3px;background:rgb(0,0,0,0.65);">\
                      <i style="position:relative;display:inline-block;margin-left:20px;width:30px;height:30px;background:green; border-radius:50%;">\
                        <i style="position:absolute;top:30%;left:22%;display:inline-block;width:15px;height:8px;border-left:3px solid #fff;border-bottom:3px solid #fff;transform:rotate(-45deg);"></i>\
                      </i>\
                      <span id="success-tip" style="font-size:16px;color:#fff;margin-left:10px;padding-right:10px;">成功~<span/>\
                    </div>';

  document.body.appendChild(toast);
  document.getElementById(TAOSHA_TOAST).style.cssText = "display:none;position:fixed;top:20px;right:10px;z-index:2147483647;border:none;";
})();


document.onclick = function () {
  clearElement();
}

function clearElement() {
  var _taoshaScript = document.getElementById(TAOSHA_SCRIPT);
  var _taoshaLoginIframe = document.getElementById(TAOSHA_LOGIN_IFRAME);
  var _taoshaCollectIframe = document.getElementById(TAOSHA_COLLECT_IFRAME);
  var _taoshaBox = document.getElementById(TAOSHA_BOX);
  var _taoshaToast = document.getElementById(TAOSHA_TOAST);
  
  // clearTimeout(TIMETOCLOSE);

  if (_taoshaScript) {
    document.body.removeChild(_taoshaScript);
  }

  if (_taoshaLoginIframe || _taoshaCollectIframe) {
    _taoshaBox.removeChild(_taoshaLoginIframe);
    _taoshaBox.removeChild(_taoshaCollectIframe);
  }

  if (_taoshaBox) {
    document.body.removeChild(_taoshaBox);
  }

  
  if(_taoshaToast) {
    document.body.removeChild(_taoshaToast);
  }
}

function showTip(tip) {
  document.getElementById('success-tip').innerText = tip;
  document.getElementById(TAOSHA_TOAST).style.display = 'block';
  setTimeout('clearElement()', 1500);
}

// 通信监听
window.onmessage = function (e) {
  console.log(e);
  switch (e.data) {
    case 'loginRequest':
      document.getElementById(TAOSHA_LOGIN_IFRAME).style.display = 'block';
      document.getElementById(TAOSHA_COLLECT_IFRAME).style.display = 'none';
      break;
    case 'loginSuccess':
      document.getElementById(TAOSHA_BOX).style.display = 'none';
      document.getElementById(TAOSHA_LOGIN_IFRAME).style.display = 'none';
      showTip('登录成功~');
      break;
    case 'logined':
      document.getElementById(TAOSHA_TOAST).style.display = 'none';
      document.getElementById(TAOSHA_LOGIN_IFRAME).style.display = 'none';
      document.getElementById(TAOSHA_COLLECT_IFRAME).style.display = 'block';
      break;
    case 'logout':
      document.getElementById(TAOSHA_COLLECT_IFRAME).style.display = 'none'; 
      showTip('已退出');
      break;
    case 'collectSuccess': 
      document.getElementById(TAOSHA_COLLECT_IFRAME).style.display = 'none';
      showTip('收藏成功~');
      break;
    case 'cancelCollect': 
      document.getElementById(TAOSHA_COLLECT_IFRAME).style.display = 'none';
      showTip('取消收藏成功');
      break;
    case 'cancel': 
      document.getElementById(TAOSHA_BOX).style.display = 'none';
      document.getElementById(TAOSHA_COLLECT_IFRAME).style.display = 'none';
      break;
    case 'loginError':
      console.log('loginError handler');
      break;
    case 'clearTimeout':
      // clearTimeout(TIMETOCLOSE);
      break;
    default:
      break;
  }
};
